<?php
namespace app\agent\controller;

use app\admin\widget\Name;
use app\common\cache\common\IAreaCache;
use app\common\cache\loan\ILoanAgencyCache;
use app\common\cache\loan\ILoanCache;
use app\common\cache\loan\ILoanFavoriteCache;
use app\common\cache\loan\ILoanRecommendCache;
use app\common\cache\loan\ILoanTagCache;
use app\common\controller\AgentBaseController;
use app\common\enum\LoanStatus;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\ILog;
use app\common\ext\IRequest;
use think\Db;

class Loan extends AgentBaseController{

    /**
     * @功能：已上架贷款列表
     * @开发者：cxl
     * @return string
     */
    public function loan_up_list(){
        //接收参数
        $loan_agency_type = IRequest::get('loan_agency_type');
        $loan_agency_id = IRequest::get('loan_agency_id');
        $loan_product_id = IRequest::get('loan_product_id');
        $loan_product_name = IRequest::get('loan_product_name');
        $loan_status = IRequest::get('loan_status');
        $loan_pledge = IRequest::get('loan_pledge');
        $loan_repayment = IRequest::get('loan_repayment');
        $loan_identity = IRequest::get('loan_identity');

        //设置查询条件
        $loan_product_where = null;
        $loan_product_where['agent_id'] = get_login_agent_id();
        $loan_product_where['is_delete'] = IsDelete::No;
        $loan_product_where['loan_status'] = LoanStatus::release;
        if(!empty($loan_agency_type)) $loan_product_where['loan_agency_type'] = $loan_agency_type;
        if(!empty($loan_agency_id)) $loan_product_where['loan_agency_id'] = $loan_agency_id;
        if(!empty($loan_product_id)) $loan_product_where['loan_product_id'] = $loan_product_id;
        if(!empty($loan_product_name)) $loan_product_where['loan_product_name'] = array("like","%{$loan_product_name}%");
        if(!empty($loan_status) && in_array($loan_status,array(LoanStatus::release))) $loan_product_where['loan_status'] = $loan_status;
        if(!empty($loan_pledge)) $loan_product_where['loan_pledge'] = $loan_pledge;
        if(!empty($loan_repayment)) $loan_product_where['loan_repayment'] = $loan_repayment;
        if(!empty($loan_identity)) $loan_product_where['']=array("exp","find_in_set({$loan_identity}, loan_identity_list)");

        //取得管理员详情
        $loan_list = IDb::getInstance('loan_product')->setDbWhere($loan_product_where)->setDbOrder("loan_product_id desc")->pag();
        if ($loan_list === false){
            error('获取管理员列表失败');
        }

        //页面赋值
        $this->assign("loan_agency_type",$loan_agency_type);
        $this->assign("loan_agency_id",$loan_agency_id);
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_product_name",$loan_product_name);
        $this->assign("loan_status",$loan_status);
        $this->assign("loan_pledge",$loan_pledge);
        $this->assign("loan_repayment",$loan_repayment);
        $this->assign("loan_identity",$loan_identity);
        $this->assign("loan_list",$loan_list);

        //渲染模板输出
        return view("loan_up_list");
    }

    /**
     * @功能：编辑中贷款产品列表
     * @开发者：cxl
     * @return string
     */
    public function loan_edit_list(){
        //接收参数
        $loan_agency_id = IRequest::get('loan_agency_id');
        $loan_product_id = IRequest::get('loan_product_id');
        $loan_product_name = IRequest::get('loan_product_name');
        $loan_status = IRequest::get('loan_status');
        $loan_pledge = IRequest::get('loan_pledge');
        $loan_repayment = IRequest::get('loan_repayment');
        $loan_identity = IRequest::get('loan_identity');

        //设置查询条件
        $loan_product_where = null;
        $loan_product_where['agent_id'] = get_login_agent_id();
        $loan_product_where['is_delete'] = IsDelete::No;
        $loan_product_where['loan_status'] = array("in",array(loanStatus::edit,loanStatus::audit,loanStatus::audit_failure,loanStatus::down));
        if(!empty($loan_agency_id)) $loan_product_where['loan_agency_id'] = $loan_agency_id;
        if(!empty($loan_product_id)) $loan_product_where['loan_product_id'] = $loan_product_id;
        if(!empty($loan_product_name)) $loan_product_where['loan_product_name'] = array("like","%{$loan_product_name}%");
        if(!empty($loan_status) && !in_array($loan_status,array(loanStatus::release))) $loan_product_where['loan_status'] = $loan_status;
        if(!empty($loan_pledge)) $loan_product_where['loan_pledge'] = $loan_pledge;
        if(!empty($loan_repayment)) $loan_product_where['loan_repayment'] = $loan_repayment;
        if(!empty($loan_identity)) $loan_product_where[''] = array("exp","find_in_set({$loan_identity}, loan_identity_list)");

        //取得管理员详情
        $loan_list = IDb::getInstance('loan_product')->setDbWhere($loan_product_where)->setDbOrder("loan_product_id desc")->pag();
        if ($loan_list === false){
            error('获取管理员列表失败');
        }

        //页面赋值
        $this->assign("loan_agency_id",$loan_agency_id);
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_product_name",$loan_product_name);
        $this->assign("loan_status",$loan_status);
        $this->assign("loan_pledge",$loan_pledge);
        $this->assign("loan_repayment",$loan_repayment);
        $this->assign("loan_identity",$loan_identity);
        $this->assign("loan_list",$loan_list);

        //渲染模板输出
        return view("loan_edit_list");
    }

    /**
     * @功能：已删除贷款列表
     * @开发者：cxl
     * @return string
     */
    public function loan_del_list(){
        //接收参数
        $loan_product_id = IRequest::get('loan_product_id');
        $loan_product_name = IRequest::get('loan_product_name');
        $loan_status = IRequest::get('loan_status');
        $loan_pledge = IRequest::get('loan_pledge');
        $loan_repayment = IRequest::get('loan_repayment');
        $loan_identity = IRequest::get('loan_identity');

        //设置查询条件
        $loan_product_where['is_delete'] = IsDelete::Yes;
        $loan_product_where['agent_id'] = get_login_agent_id();
        if(!empty($loan_product_id)) $loan_product_where['loan_product_id'] = $loan_product_id;
        if(!empty($loan_product_name)) $loan_product_where['loan_product_name'] = array("like","%{$loan_product_name}%");
        if(!empty($loan_status)) $loan_product_where['loan_status'] = $loan_status;
        if(!empty($loan_pledge)) $loan_product_where['loan_pledge'] = $loan_pledge;
        if(!empty($loan_repayment)) $loan_product_where['loan_repayment'] = $loan_repayment;
        if(!empty($loan_identity)) $loan_product_where[''] =  array("exp","find_in_set({$loan_identity}, loan_identity_list)");

        //取得管理员详情
        $loan_list = IDb::getInstance('loan_product')->setDbWhere($loan_product_where)->setDbOrder("loan_product_id desc")->pag();
        if ($loan_list === false){
            error('获取管理员列表失败');
        }

        //页面赋值
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_product_name",$loan_product_name);
        $this->assign("loan_status",$loan_status);
        $this->assign("loan_pledge",$loan_pledge);
        $this->assign("loan_repayment",$loan_repayment);
        $this->assign("loan_identity",$loan_identity);
        $this->assign("loan_list",$loan_list);

        //渲染模板输出
        return view("loan_del_list");
    }

    /**
     * @功能：贷款详情
     * @开发者：cxl
     * @return string
     */
    public function loan_info(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得贷款信息
        $loan_info = ILoanCache::getInfo($loan_product_id);

        //判断详情数据是否存在
        $product_area_list = array();
        if(!empty($loan_info)){
            //整理币种
            $loan_info['loan_content_img'] = explode(",",$loan_info['loan_content_img']);
            $loan_info['loan_identity_list'] = explode(",",$loan_info['loan_identity_list']);

            //取得所在区域信息
            $loan_product_area_where = null;
            $loan_product_area_where['loan_product_id'] = $loan_product_id;
            $loan_product_area_where['province'] = array("exp","is not null");
            $loan_product_area_where['city'] = array("exp","is not null");
            $product_area_list = IDb::getInstance('loan_product_area')->setDbFiled("province,city")->setDbWhere($loan_product_area_where)->sel();
            if($product_area_list === false){
                $product_area_list = array();
            }
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_info",$loan_info);
        $this->assign("area_list",$area_list);
        $this->assign("product_area_list",$product_area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷款日志
     * @开发者：cxl
     * @return string
     */
    public function loan_log(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");

        //取得日志
        $loan_product_log_where['loan_product_id'] = $loan_product_id;
        $loan_product_log_list = IDb::getInstance('loan_product_log')->setDbWhere($loan_product_log_where)->setDbOrder("loan_product_log_id desc")->pag();

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("loan_product_log_list",$loan_product_log_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加贷款
     * @开发者：cxl
     * @return string
     */
    public function loan_add(){

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //页面赋值
        $this->assign("area_list",$area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：修改贷款
     * @开发者：cxl
     * @return string
     */
    public function loan_edit(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);;

        //取得贷款信息
        $loan_info = ILoanCache::getInfo($loan_product_id);

        //判断详情数据是否存在
        $product_area_list = array();
        if(!empty($loan_info)){
            //整理数据
            $loan_info['loan_tag_ary'] = explode(",",$loan_info['loan_tag_str']);
            $loan_info['loan_identity_list'] = explode(",",$loan_info['loan_identity_list']);
            $loan_info['loan_content_img'] = explode(",",$loan_info['loan_content_img']);

            //取得所在区域信息
            $loan_product_area_where = null;
            $loan_product_area_where['loan_product_id'] = $loan_product_id;
            $loan_product_area_where['province'] = array("exp","is not null");
            $loan_product_area_where['city'] = array("exp","is not null");
            $product_area_list = IDb::getInstance('loan_product_area')->setDbFiled("province,city")->setDbWhere($loan_product_area_where)->sel();
            if($product_area_list === false){
                $product_area_list = array();
            }
        }

        //页面赋值
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_info",$loan_info);
        $this->assign("area_list",$area_list);
        $this->assign("product_area_list",$product_area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：添加贷款
     * @开发者：cxl
     * @return string
     */
    public function loan_add_submit(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id");
        $loan_product_name = IRequest::get("loan_product_name",IRequest::NOT_EMPTY,"贷款产品名称不能为空！");
        $loan_agency_name = IRequest::get("loan_agency_name",IRequest::NOT_EMPTY,"贷款产品机构不能为空！");
        $loan_product_outline = IRequest::get("loan_product_outline",IRequest::NOT_EMPTY,"贷款产品概要不能为空！");
        $loan_pledge = IRequest::get("loan_pledge",IRequest::NOT_EMPTY,"抵押方式不能为空！");
        $loan_repayment = IRequest::get("loan_repayment",IRequest::NOT_EMPTY,"还款方式不能为空！");
        $is_car = IRequest::get("is_car",IRequest::NOT_EMPTY,"是否要求有车不能为空！");
        $is_policy = IRequest::get("is_policy",IRequest::NOT_EMPTY,"是否要求有保单不能为空！");
        $is_house = IRequest::get("is_house",IRequest::NOT_EMPTY,"本市是否有房产不能为空！");
        $loan_identity = IRequest::get("loan_identity");
        $add_list = IRequest::get("add_list");
        $area = IRequest::get("area");
        $min_loan_money = IRequest::get("min_loan_money",IRequest::NOT_EMPTY,"最小贷款金额不能为空！");
        $max_loan_money = IRequest::get("max_loan_money",IRequest::NOT_EMPTY,"最大贷款金额不能为空！");
        $min_period = IRequest::get("min_period",IRequest::NOT_EMPTY,"最小贷款期限不能为空！");
        $max_period = IRequest::get("max_period",IRequest::NOT_EMPTY,"最大贷款期限不能为空！");
        $month_rate = IRequest::get("month_rate",IRequest::NOT_EMPTY,"月利率不能为空！");
        $nonrecurring_rate = IRequest::get("nonrecurring_rate",IRequest::NOT_EMPTY,"一次性费用不能为空！");
        $loan_content_txt = IRequest::get("loan_content_txt");
        $loan_content_img = IRequest::get("loan_content_img");
        $order_by = IRequest::get("order_by");
        $remark = IRequest::get("remark");

        if (empty($loan_identity)){
            error('职业身份要求不能为空！');
        }

        //处理详情图片
        $loan_content_img = implode( ',', $loan_content_img);

        //处理自定义标签
        if (strlen($add_list)>=2){
            $add_list = substr($add_list,0,strlen($add_list)-1);
        }else{
            $add_list = null;
        }

        //判断一次性和月利率是否正确
        $nonrecurring_rate = floatval($nonrecurring_rate);
        $month_rate = floatval($month_rate);
        if(100 < $nonrecurring_rate || $nonrecurring_rate < 0){
            error("一次性费用输入错误！");
        }

        if(100 < $month_rate || $month_rate < 0){
            error("月利率输入错误！");
        }

        //判断标签是否合理
        if (!empty($add_list)){
            $add_ary = explode(",", $add_list);
            if (count($add_ary)>=6){
                //提示错误信息
                error("标签最多只能6个！");
            }

            foreach ($add_ary as $k=>$v){
                if(mb_strlen($v)<4||mb_strlen($v)>7){
                    //提示错误信息
                    error("每个标签最多只能4到7个字！");
                }
            }
        }

        //判断金额是否正确
        $min_loan_money = floatval($min_loan_money);
        $max_loan_money = floatval($max_loan_money);
        if($max_loan_money < $min_loan_money || $min_loan_money < 0){
            error("最大最小贷款金额输入错误！");
        }

        //判断期限是否正确
        $min_period = intval($min_period);
        $max_period = intval($max_period);
        if($max_period < $min_period || $min_period < 0){
            error("最大最小贷款期限输入错误！");
        }

        //职业身份要求
        $loan_identity_data = array();
        if(is_array($loan_identity) && count($loan_identity) > 0){
            $loan_identity_data = $loan_identity;
        }


        //开启事物处理
        IDb::dbStartTrans();

        //判断是否为新添加数据
        if(empty($loan_product_id)) {
            //添加贷款
            $loan_data = null;
            $loan_data['agent_id'] = get_login_agent_id();
            $loan_data['agent_admin_id'] = get_login_agent_admin_id();
            $loan_data['loan_product_name'] = $loan_product_name;
            $loan_data['loan_agency_name'] = $loan_agency_name;
            $loan_data['loan_product_outline'] = $loan_product_outline;
            $loan_data['loan_pledge'] = $loan_pledge;
            $loan_data['loan_repayment'] = $loan_repayment;
            $loan_data['is_car'] = $is_car;
            $loan_data['is_policy'] = $is_policy;
            $loan_data['is_house'] = $is_house;
            $loan_data['loan_identity_list'] = join(",",$loan_identity_data);
            $loan_data['loan_tag_str'] = $add_list;
            $loan_data['min_loan_money'] = $min_loan_money;
            $loan_data['max_loan_money'] = $max_loan_money;
            $loan_data['min_period'] = $min_period;
            $loan_data['max_period'] = $max_period;
            $loan_data['month_rate'] = $month_rate;
            $loan_data['nonrecurring_rate'] = $nonrecurring_rate;
            $loan_data['loan_content_txt'] = $loan_content_txt;
            $loan_data['loan_content_img'] = $loan_content_img;
            $loan_data['order_by'] = intval($order_by);
            $loan_data['remark'] = $remark;
            $loan_data['loan_status'] = LoanStatus::edit;
            $loan_data['create_time'] = get_date_time();
            $loan_data['is_delete'] = IsDelete::No;
            $loan_add = IDb::getInstance('loan_product')->setDbData($loan_data)->add();
            if ($loan_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加贷款基础信息失败，请联系管理员！");
            }

            //添加操作日志
            $loan_log_add = ILog::LoanLog($loan_add,null,get_login_agent_admin_id(),"创建贷款！");
            if ($loan_log_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加贷款基础信息失败，请联系管理员！");
            }

            //设置贷款编号
            $loan_product_id = $loan_add;
        }else{
            //添加贷款
            $loan_data = null;
            $loan_where = null;
            $loan_where['loan_product_id'] = $loan_product_id;
            $loan_data['loan_product_name'] = $loan_product_name;
            $loan_data['loan_agency_name'] = $loan_agency_name;
            $loan_data['loan_product_outline'] = $loan_product_outline;
            $loan_data['loan_pledge'] = $loan_pledge;
            $loan_data['loan_repayment'] = $loan_repayment;
            $loan_data['is_car'] = $is_car;
            $loan_data['is_policy'] = $is_policy;
            $loan_data['is_house'] = $is_house;
            $loan_data['loan_identity_list'] = join(",",$loan_identity_data);
            $loan_data['loan_tag_str'] = $add_list;
            $loan_data['min_loan_money'] = $min_loan_money;
            $loan_data['max_loan_money'] = $max_loan_money;
            $loan_data['min_period'] = $min_period;
            $loan_data['max_period'] = $max_period;
            $loan_data['month_rate'] = $month_rate;
            $loan_data['nonrecurring_rate'] = $nonrecurring_rate;
            $loan_data['loan_content_txt'] = $loan_content_txt;
            $loan_data['loan_content_img'] = $loan_content_img;
            $loan_data['order_by'] = intval($order_by);
            $loan_data['remark'] = $remark;
            $loan_data['loan_status'] = LoanStatus::edit;
            $loan_data['update_time'] = get_date_time();
            $loan_data['is_delete'] = IsDelete::No;
            $loan_upd = IDb::getInstance('loan_product')->setDbData($loan_data)->setDbWhere($loan_where)->upd();
            if ($loan_upd === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加贷款基础信息失败，请联系管理员！");
            }

            //添加操作日志
            $loan_log_add = ILog::LoanLog($loan_product_id,null,get_login_agent_admin_id(),"修改贷款！");
            if ($loan_log_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加贷款基础信息失败，请联系管理员！");
            }
        }

        //删除历史效区域数据
        $loan_area_where['loan_product_id'] = $loan_product_id;
        $loan_area_del = IDb::getInstance('loan_product_area')->setDbWhere($loan_area_where)->del();
        if ($loan_area_del === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("设置贷款有效区域失败！");
        }

        //判断贷款有效区域
        if(is_array($area) && count($area) > 0){
            //去掉重复数据
            $area = array_unique($area);

            //循环写数据
            foreach($area as $item){
                //拆分省份及城市
                list($province,$city) = explode(",",$item);
                if(empty($province) || empty($city)){
                    //事物回滚
                    IDb::dbRollback();

                    //提示错误信息
                    error("设置贷款有效区域失败！");
                }

                //写有效区域数据
                $loan_area_data = null;
                $loan_area_data['loan_product_id'] = $loan_product_id;
                $loan_area_data['province'] = $province;
                $loan_area_data['city'] = $city;
                $loan_area_data['create_time'] = get_date_time();
                $loan_area_add = IDb::getInstance('loan_product_area')->setDbData($loan_area_data)->add();
                if ($loan_area_add === false) {
                    //事物回滚
                    IDb::dbRollback();

                    //提示错误信息
                    error("设置贷款有效区域失败！");
                }
            }
        }else{
            //写有效区域数据
            $loan_area_data = null;
            $loan_area_data['loan_product_id'] = $loan_product_id;
            $loan_area_data['province'] = null;
            $loan_area_data['city'] = null;
            $loan_area_data['create_time'] = get_date_time();
            $loan_area_add = IDb::getInstance('loan_product_area')->setDbData($loan_area_data)->add();
            if ($loan_area_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("设置贷款有效区域失败！");
            }
        }

        //提交事物处理
        IDb::dbCommit();

        //清除缓存
        ILoanCache::deleteInfo($loan_product_id);
        ILoanCache::deleteList();
        ILoanRecommendCache::deleteList();
        ILoanFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！",Url("loan_edit_list"));
    }

    /**
     * @功能：提交审核
     * @开发者：cxl
     * @return string
     */
    public function loan_commit(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");

        //开启事物
        IDb::dbStartTrans();

        //修改管理员信息
        $loan_product_where['loan_product_id'] = $loan_product_id;
        $loan_product_data['loan_status'] = loanStatus::audit;
        $loan_product_data['update_time'] = get_date_time();
        $loan_upd = IDb::getInstance('loan_product')->setDbWhere($loan_product_where)->setDbData($loan_product_data)->upd();
        if($loan_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("贷款提交审核失败！");
        }

        //添加操作日志
        $loan_log_add = ILog::LoanLog($loan_product_id,null,get_login_agent_admin_id(),"贷款提交审核！");
        if ($loan_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("贷款提交审核失败！");
        }

        //提交事物处理
        IDb::dbCommit();

        //清除缓存
        ILoanCache::deleteInfo($loan_product_id);
        ILoanCache::deleteList();
        ILoanRecommendCache::deleteList();
        ILoanFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：贷款审核
     * @开发者：cxl
     * @return string
     */
    public function loan_audit(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");

        //取得贷款机构表
        $loan_agency_list = ILoanAgencyCache::getList();

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //取得贷款信息
        $loan_info = ILoanCache::getInfo($loan_product_id);

        //判断详情数据是否存在
        $product_area_list = array();
        if(!empty($loan_info)){
            //整理数据
            $loan_info['loan_apply_condition_str'] = json_decode($loan_info['loan_apply_condition_str'],true);
            $loan_info['loan_apply_material_str'] = json_decode($loan_info['loan_apply_material_str'],true);
            $loan_info['rate_info'] = json_decode($loan_info['rate_info'],true);

            //整理币种
            $loan_info['loan_house_list'] = explode(",",$loan_info['loan_house_list']);
            $loan_info['loan_identity_list'] = explode(",",$loan_info['loan_identity_list']);

            //取得所在区域信息
            $loan_product_area_where = null;
            $loan_product_area_where['loan_product_id'] = $loan_product_id;
            $loan_product_area_where['province'] = array("exp","is not null");
            $loan_product_area_where['city'] = array("exp","is not null");
            $product_area_list = IDb::getInstance('loan_product_area')->setDbFiled("province,city")->setDbWhere($loan_product_area_where)->sel();
            if($product_area_list === false){
                $product_area_list = array();
            }
        }

        //页面赋值
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_info",$loan_info);
        $this->assign("area_list",$area_list);
        $this->assign("loan_agency_list",$loan_agency_list);
        $this->assign("product_area_list",$product_area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷款审核提交
     * @开发者：cxl
     * @return string
     */
    public function loan_audit_submit(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");
        $loan_status = IRequest::get("loan_status",IRequest::NOT_EMPTY,"贷款编号不能为空！");
        $audit_note = IRequest::get("audit_note");

        //开启事物
        IDb::dbStartTrans();

        //修改管理员信息
        $loan_product_where['loan_product_id'] = $loan_product_id;
        $loan_product_data['loan_status'] = $loan_status;
        $loan_product_data['audit_note'] = $audit_note;
        $loan_product_data['update_time'] = get_date_time();
        $loan_upd = IDb::getInstance('loan_product')->setDbWhere($loan_product_where)->setDbData($loan_product_data)->upd();
        if($loan_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("贷款审核错误！");
        }

        //添加操作日志
        $loan_log_add = ILog::LoanLog($loan_product_id,null,get_login_agent_admin_id(),"贷款审核！");
        if ($loan_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("贷款审核错误！");
        }

        //提交事物处理
        IDb::dbCommit();

        //清除缓存
        ILoanCache::deleteInfo($loan_product_id);
        ILoanCache::deleteList();
        ILoanCache::deletePcList();
        ILoanRecommendCache::deleteList();
        ILoanFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！",Url("loan_edit_list"));
    }

    /**
     * @功能：贷款下架
     * @开发者：cxl
     * @return string
     */
    public function loan_down(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $loan_product_where['loan_product_id'] = $loan_product_id;
        $loan_product_data['loan_status'] = loanStatus::down;
        $loan_product_data['update_time'] = get_date_time();
        $loan_upd = IDb::getInstance('loan_product')->setDbWhere($loan_product_where)->setDbData($loan_product_data)->upd();
        if($loan_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("贷款下架失败！");
        }

        //修改管理员信息
        $loan_recommend_where['loan_product_id'] = $loan_product_id;
        $loan_recommend_data['is_delete'] = IsDelete::Yes;
        $loan_recommend_data['update_admin'] = get_login_agent_admin_id();
        $loan_recommend_data['update_time'] = get_date_time();
        $loan_recommend_upd = IDb::getInstance('loan_recommend')->setDbWhere($loan_recommend_where)->setDbData($loan_recommend_data)->upd();
        if($loan_recommend_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("贷款推荐删除失败！");
        }

        //添加操作日志
        $loan_log_add = ILog::LoanLog($loan_product_id,null,get_login_agent_admin_id(),"贷款下架！");
        if ($loan_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("贷款下架失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        ILoanCache::deleteInfo($loan_product_id);
        ILoanCache::deleteList();
        ILoanCache::deletePcList();
        ILoanRecommendCache::deleteList();
        ILoanFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：贷款删除
     * @开发者：cxl
     * @return string
     */
    public function loan_del(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $loan_product_where['loan_product_id'] = $loan_product_id;
        $loan_product_data['is_delete'] = IsDelete::Yes;
        $loan_product_data['update_time'] = get_date_time();
        $loan_upd = IDb::getInstance('loan_product')->setDbWhere($loan_product_where)->setDbData($loan_product_data)->upd();
        if($loan_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("贷款删除失败！");
        }

        //修改管理员信息
        $loan_recommend_where['loan_product_id'] = $loan_product_id;
        $loan_recommend_data['is_delete'] = IsDelete::Yes;
        $loan_recommend_data['update_admin'] = get_login_agent_admin_id();
        $loan_recommend_data['update_time'] = get_date_time();
        $loan_recommend_upd = IDb::getInstance('loan_recommend')->setDbWhere($loan_recommend_where)->setDbData($loan_recommend_data)->upd();
        if($loan_recommend_upd === false){
            //事物回滚
            IDb::dbRollback();

            //错误信息
            error("贷款删除失败！");
        }

        //添加操作日志
        $loan_log_add = ILog::LoanLog($loan_product_id,null,get_login_agent_admin_id(),"贷款删除！");
        if ($loan_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("贷款删除失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        ILoanCache::deleteInfo($loan_product_id);
        ILoanCache::deleteList();
        ILoanRecommendCache::deleteList();
        ILoanFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：贷款还原
     * @开发者：cxl
     * @return string
     */
    public function loan_recycle(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");

        //开启事物出来
        IDb::dbStartTrans();

        //修改管理员信息
        $loan_product_where['loan_product_id'] = $loan_product_id;
        $loan_product_data['is_delete'] = IsDelete::No;
        $loan_product_data['update_time'] = get_date_time();
        $loan_upd = IDb::getInstance('loan_product')->setDbWhere($loan_product_where)->setDbData($loan_product_data)->upd();
        if($loan_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("贷款还原失败！");
        }

        //添加操作日志
        $loan_log_add = ILog::LoanLog($loan_product_id,null,get_login_agent_admin_id(),"贷款还原！");
        if ($loan_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("贷款还原失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        ILoanCache::deleteInfo($loan_product_id);
        ILoanCache::deleteList();
        ILoanRecommendCache::deleteList();
        ILoanFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：贷款编辑标签
     * @开发者：cxl
     * @return string
     */
    public function loan_tag(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");

        //取得标签列表
        $loan_tag_list = ILoanTagCache::getList();
        if($loan_tag_list === false){
            error("取得标签列表失败！");
        }

        //取得标签
        $loan_product_info = ILoanCache::getInfo($loan_product_id);
        if($loan_product_info === false){
            error("取得标签列表失败！");
        }

        //整理标签信息
        $loan_tag = array();
        if(!empty($loan_product_info['loan_tag_str'])){
            $loan_tag = explode(",",$loan_product_info['loan_tag_str']);
        }

        //整理自定义标签
        if (!empty($loan_product_info['loan_ext_tag_str'])){
            $loan_product_info['loan_ext_tag_ary'] = explode(",",$loan_product_info['loan_ext_tag_str']);
        }


        //页面赋值
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_product_info",$loan_product_info);
        $this->assign("loan_tag_list",$loan_tag_list);
        $this->assign("loan_tag",$loan_tag);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷款编辑标签
     * @开发者：cxl
     * @return string
     */
    public function loan_tag_submit(){
        //接收参数
        $loan_product_id = IRequest::get("loan_product_id",IRequest::NOT_EMPTY,"贷款编号不能为空！");
        $loan_tag_array = IRequest::get("loan_tag_id");
        $add_list = IRequest::get("add_list");

        //处理自定义标签
        if (strlen($add_list)>=2){
            $add_list = substr($add_list,0,strlen($add_list)-1);
        }else{
            $add_list = null;
        }

        //判断是是否为数组
        if(!is_array($loan_tag_array) || empty($loan_tag_array)){
            error("请选择贷款标签！");
        }

        //开启事物出来
        IDb::dbStartTrans();

        //添加新标签列表
        $loan_product_where['loan_product_id'] = $loan_product_id;
        $loan_product_data['loan_tag_str'] = join(",",$loan_tag_array);
        $loan_product_data['loan_ext_tag_str'] = $add_list;
        $loan_product_data['update_time'] = get_date_time();
        $loan_product_upd = IDb::getInstance('loan_product')->setDbData($loan_product_data)->setDbWhere($loan_product_where)->upd();
        if($loan_product_upd === false){
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("保存贷款标签失败！");
        }

        //添加操作日志
        $loan_log_add = ILog::LoanLog($loan_product_id,null,get_login_agent_admin_id(),"修改贷款标签！");
        if ($loan_log_add === false) {
            //事物回滚
            IDb::dbRollback();

            //提示错误信息
            error("修改贷款标签失败！");
        }

        //事物提交
        IDb::dbCommit();

        //清除缓存
        ILoanCache::deleteInfo($loan_product_id);
        ILoanCache::deleteList();
        ILoanRecommendCache::deleteList();
        ILoanFavoriteCache::deleteListAll();

        //画面跳转
        $this->success("操作成功！",Url("loan_edit_list"));
    }

    /**
     * @功能：贷款分享记录
     * @开发者：cxl
     * @return string
     */
    public function loan_share_log(){
        //接收参数
        $user_id = IRequest::get("user_id");
        $loan_product_id = IRequest::get("loan_product_id");

        //取得数据
        $loan_share_where = null;
        $loan_share_where['cp.agent_id'] = get_login_agent_id();
        if(!empty($user_id)) $loan_share_where['cs.user_id'] = $user_id;
        if(!empty($loan_product_id)) $loan_share_where['cs.loan_product_id'] = $loan_product_id;
        $loan_share_list = IDb::getInstance("loan_share as cs")
            ->setDbFiled('cs.*')
            ->setDbJoin('loan_product as cp', 'cp.loan_product_id = cs.loan_product_id')
            ->setDbWhere($loan_share_where)
            ->setDbOrder('cs.share_id desc')
            ->pag();
        if ($loan_share_list === false){
            error('获取贷款分享记录失败');
        }

        //页面赋值
        $this->assign("user_id",$user_id);
        $this->assign("loan_product_id",$loan_product_id);
        $this->assign("loan_share_list",$loan_share_list);

        //渲染模板输出
        return view();
    }
}
